﻿using ETModel;
using UnityEngine;

namespace ETHotfix {
    [ObjectSystem]
    public class FollowUnitCameraComponentAwakeSystem : AwakeSystem<FollowUnitCameraComponent> {
        public override void Awake(FollowUnitCameraComponent self) {
            self.Awake();
        }
    }
    [ObjectSystem]
    public class FollowUnitCameraComponentStartSystem : StartSystem<FollowUnitCameraComponent> {
        public override void Start(FollowUnitCameraComponent self) {
            self.Start();
        }
    }

    [ObjectSystem]
    public class FollowUnitCameraComponentLateUpdateSystem : LateUpdateSystem<FollowUnitCameraComponent> {
        public override void LateUpdate(FollowUnitCameraComponent self) {
            self.LateUpdate();
        }
    }

    /// <summary>
    /// 斜俯视跟随单位的相机(简单实现),附加到场景即可使用
    /// </summary>
    public class FollowUnitCameraComponent : Component {

        // 战斗摄像机
        public Camera mainCamera;

        private Vector3 _followOffset;

        public Unit FollowUnit { get; set; }

        /// <summary>
        /// 相机高度,默认10
        /// </summary>
        public float CameraHeight { get; set; } = 10;

        public void Awake() {
            this.mainCamera = Camera.main;
        }
        public void Start() {
            //计算出中心点
            _followOffset = mainCamera.transform.position - (mainCamera.transform.position + mainCamera.transform.forward * CameraHeight);
        }

        public void LateUpdate() {
            // 摄像机每帧更新位置
            UpdatePosition();
        }

        private void UpdatePosition() {
            if (FollowUnit != null && !FollowUnit.IsDisposed) {
                mainCamera.transform.position = FollowUnit.Position + _followOffset;
            }
        }
    }
}
